Ext.define('Admin.view.main.factory.ModelFactory', {
    statics: {
        getModelByName: function (name) {
            var module = Ext.StoreMgr.get('Models').getById(name);
            return Ext.define("Admin.model." + (module.get('name')), {
                extend: 'Admin.model.Base',
                idProperty: module.get('id_property'),
                nameField: module.get('name_field'),
                fields: this.getFields(module),
                getIdValue: function () {
                    return this.get(this.idProperty);
                },
                getNameValue: function () {
                    if (this.nameField) {
                        return this.get(this.nameField);
                    } else {
                        return null;
                    }
                }
            });
        },

        getFields: function (module) {
            var columns = module.get('columns'),
                fields = [],
                field, i;
            for (i = 0; i < columns.length; i++) {
                field = columns[i];
                fields.push({
                    name: field.name,
                    type: field.type || 'auto'
                });
            }
            return fields;
        }
    }
});
